//通用代码阶级
var phaseList;
// 通用代码密级
var secretList;
// 保管期限
var limitList;
var sysId;
// 记录开始新增项目编辑行数
var beginRowCount = 1;
// 普通全局变量
var planIds = [];
var projectIds = [];
var projectNames = [];
var numberS = [];

// 打开添加计划弹出框
function opean() {
    $("#dd").dialog('open');
    $("#QQ").dialog('refresh');
}

// 打开添加项目弹出框
function opeanProject() {
    $("#QQ").dialog('open');
    var row = $("#dataTable").datagrid('getSelected');
    var rows = $('#dataTable').datagrid('getSelections');
    if (rows.length > 1) {
        $("#QQ").dialog('close');
        delete rows;
        $.messager.alert("提示", "请最多选择一条数据，才可进行该操作！", "warning");
    } else if (rows == null || rows.length <= 0) {
        $("#QQ").dialog('close');
        delete rows;
        $.messager.alert("提示", "请至少选择一条数据，才可进行该操作！", "warning");
    }
    if (row != null || row.length > 0) {
        $("#planNameto").val(row.ARCHIVE_FILE_KIND_NAME);
        $("#planId").val(row.ID);
        selectProject();
    }
}

// 编辑项目
function editProject() {
    var rows = $('#ProjectTable').datagrid('getRows');
    for (var i = 0; i < rows.length; i++) {
        // alert("开始编辑.index是"+rows[i].numCount);
        $("#ProjectTable").datagrid('beginEdit', rows[i].numCount - 1);
        // alert(rows[i].ID);
        projectIds.push(rows[i].ID);
        // alert(rows[i].ARCHIVE_FILE_KIND_NAME);
        numberS.push(rows[i].numCount - 1);
    }
}

function editSave(index) {
    $('#ProjectTable').datagrid('endEdit', index);
}

function sleep(delay) {
    var start = (new Date()).getTime();
    while ((new Date()).getTime() - start < delay) {
        continue;
    }
}

// 保存编辑项目和新增项目公用方法
function updateProjct() {
    $('#ProjectTable').datagrid('reload');
    var arg = '';
    var rows = $('#ProjectTable').datagrid('getRows');
    $('#ProjectTable').datagrid('endEdit', rows.length - 1);
    var dataTableRows = $('#dataTable').datagrid('getRows');
    var data = $('#dataTable').datagrid('getChecked');
    for (var i = 0; i < dataTableRows.length; i++) {
        // 是否为同名计划
        if (data[0].ARCHIVE_FILE_KIND_NAME == $("#planNameto").val()) { // 同名计划未做修改，直接通过
            break;
        }
        if (dataTableRows[i].ARCHIVE_FILE_KIND_NAME == $("#planNameto").val()) { // 同名计划与其他计划一致时，不通过
            opeanProject();
            $.messager.alert("提示", "不能保存同名的计划", "warning");
            return;
        }
    }
    // var rowSel = $('#ProjectTable').datagrid('getSelections');
    if (projectIds.length == 0) {
        for (var i = 0; i < rows.length; i++) {
            editSave(i);
        }
        for (var i = beginRowCount; i < rows.length; i++) {
            if (rows[i].ARCHIVE_FILE_KIND_NAME == "") {
                opeanProject();
                $.messager.alert("提示", "新增项目名称不能为空", "warning");
                return;
            }
            if (i == rows.length - 1) {
                arg += "'" + rows[i].ID + "'," + rows[i].ARCHIVE_FILE_KIND_NAME
                    + "'";
            } else {
                arg += "'" + rows[i].ID + "','"
                    + rows[i].ARCHIVE_FILE_KIND_NAME + "'," + "/";
            }
        }
        for (var i = 0; i < rows.length - 1; i++) {
            var name = rows[i].ARCHIVE_FILE_KIND_NAME;
            for (var y = i + 1; y < rows.length; y++) {
                var nameTo = rows[y].ARCHIVE_FILE_KIND_NAME;
                if (name == nameTo) {
                    opeanProject();
                    $.messager.alert("提示", "不能保存同名的项目", "warning");
                    return;
                }
            }
        }

        $
            .ajax({
                url: 'platform/archivetemplatemanageController/saveProjectAndPlan?sysId='
                    + sysId,
                type: 'post',
                dataType: 'json',
                data: {
                    "planId": $("#planId").val(),
                    "commitTypeto": $("#commitTypeto").val(),
                    "projectName": arg,
                    "planNameto": $("#planNameto").val(),
                },
                success: function (data) {
                    if (data.success == '1') {
                        projectIds.splice(0, projectIds.length);
                        projectNames.splice(0, projectNames.length);
                        numberS.splice(0, numberS.length);
                        beginRowCount = 1;
                        $('#QQ').dialog('close');
                        $('#dataTable').datagrid('reload');
                        parent.parent.parent.$.messager.show({
                            //消息
                            //保存成功!
                           /* title: commonLang.tip.message,*/
                        	title:"消息",
                            msg: "保存成功",
                            timeout: 500,
                            showType: 'slide'
                        });
                    } else if (data.success == '0') {
                        beginRowCount = 1;
                        $('#QQ').dialog('close');
                        parent.parent.parent.$.messager.show({
                            //消息
                            //保存失败!
                            /*title: commonLang.tip.message,*/
                        	title:"消息",
                            msg: "保存失败",
                            timeout: 500,
                            showType: 'slide'
                        });
                    }
                }
            });
    }
    if (projectIds.length > 0) {
        for (var i = 0; i < rows.length; i++) {
            editSave(i);
        }
        var arg = '';
        for (var i = beginRowCount; i < rows.length; i++) {
            if (rows[i].ARCHIVE_FILE_KIND_NAME == "") {
                opeanProject();
                $.messager.alert("提示", "新增项目名称不能为空", "warning");
                return;
            }
        }
        // 获取编辑选中的数据
        for (var i = 0; i < rows.length; i++) {
            editSave(rows[i].numCount - 1);
            if (i == rows.length - 1) {
                arg += "'" + rows[i].ID + "'," + rows[i].ARCHIVE_FILE_KIND_NAME
                    + "'";
            } else {
                arg += "'" + rows[i].ID + "','"
                    + rows[i].ARCHIVE_FILE_KIND_NAME + "'," + "/";
            }
        }
        for (var i = 0; i < rows.length - 1; i++) {
            var name = rows[i].ARCHIVE_FILE_KIND_NAME;
            for (var y = i + 1; y < rows.length; y++) {
                var nameTo = rows[y].ARCHIVE_FILE_KIND_NAME;
                if (name == nameTo) {
                    opeanProject();
                    $.messager.alert("提示", "不能保存同名的项目", "warning");
                    return;
                }
            }
        }
        $
            .ajax({
                type: 'post',
                dataType: 'json',
                url: 'platform/archivetemplatemanageController/saveProjectAndPlan?sysId='
                    + sysId,
                data: {
                    "planId": $("#planId").val(),
                    "commitTypeto": $("#commitTypeto").val(),
                    "Id": arg,
                    "planNameto": $("#planNameto").val(),
                },
                success: function (data) {
                    if (data.success == '1') {
                        projectIds.splice(0, projectIds.length);
                        projectNames.splice(0, projectNames.length);
                        numberS.splice(0, numberS.length);
                        $('#QQ').dialog('close');
                        beginRowCount = 1;
                        $('#dataTable').datagrid('reload');
                        parent.parent.parent.$.messager.show({
                            //消息
                            //保存成功!
                            /*title: commonLang.tip.message,*/
                        	title:"消息",
                            msg: "保存成功",
                            timeout: 500,
                            showType: 'slide'
                        });
                    } else if (data.success == '0') {
                        $('#QQ').dialog('close');
                        beginRowCount = 1;
                        parent.parent.parent.$.messager.show({
                            //消息
                            //保存成功!
                           /* title: commonLang.tip.message,*/
                        	title:"消息",
                            msg: "保存失败",
                            timeout: 500,
                            showType: 'slide'
                        });
                    }
                }
            });
    }
}

/**
 * delete by zhaoxiaolong name : 删除计划,根据计划名称删除 方法 : deletePlan()
 * deletePlanChecked() 日期 : 2019/4/22
 */
function deletePlan() {
    var rows = $('#dataTable').datagrid('getChecked');
    if (rows.length <= 0) {
        $("#QQ").dialog('close');
        delete rows;
        beginRowCount = "";
        $.messager.alert("提示", "请选择要删除的记录？", "warning");
    }
    if (rows.length > 0) {
        var arg = "";
        for (var i = 0; i < rows.length; i++) {
            if (i == rows.length - 1) {
                arg += "'" + rows[i].ID + "'";
            } else {
                arg += "'" + rows[i].ID + "',";
            }
        }
        deletePlanChecked(arg, "1");
    }
}

function deletePlanChecked(ids, flag) {
    $.messager.confirm('确认框', "您确定要删除当前所选的数据？", function (r) {
        if (r) {
            $.ajax({
                type: 'post',
                dataType: 'json',
                url: 'platform/archivetemplatemanageController/deletePlan',
                data: {
                    "planName": ids,
                },
                success: function (data) {
                    if (data.success == '1') {
                    	parent.parent.parent.$.messager.show({
                            //消息
                            //删除成功!
                            /*title: commonLang.tip.message,*/
                    		title:"消息",
                            msg: "删除成功",
                            timeout: 500,
                            showType: 'slide'
                        });
                        var row = $('#ProjectTable').datagrid('getSelections');
                        if (row) {
                            for (var i = 0; i < row.length; i++) {
                                var rowIndex = $('#ProjectTable').datagrid(
                                    'getRowIndex', row[i]);
                                $('#ProjectTable').datagrid('deleteRow',
                                    rowIndex);
                            }
                        }
                        if (flag == "1") {
                            $('#dataTable').datagrid('reload');
                        }
                    } else if (data.success == '0') {
                    	parent.parent.parent.$.messager.show({
                            //消息
                            //删除失败!
                           /* title: commonLang.tip.message,*/
                    		title:"消息",
                            msg: "删除失败",
                            timeout: 500,
                            showType: 'slide'
                        });
                        $('#dataTable').datagrid('reload');
                        $('#ProjectTable').datagrid('reload');
                    }
                }
            });
        }
    });
}

/**
 * create by zxl 添加计划，请勿改动
 */
function savePlan() {
    var rows = $('#dataTable').datagrid('getRows');
    var name = $('#planName').val();
    if (name == "") {
    	parent.parent.parent.$.messager.show({
            //消息
            //删除失败!
            title: "提示",
            msg: "新增计划名称不能为空",
            timeout: 1000,
            showType: 'slide'
        });
        /*$.messager.alert("提示", "新增计划名称不能为空", "warning");*/
        $("#planName").val("");
        $('#dd').dialog('close');
        return;
    }
    for (var y = 0; y < rows.length; y++) {
        var nameTo = rows[y].ARCHIVE_FILE_KIND_NAME;
        if (name == nameTo) {
            $.messager.alert("提示", "不能保存同名的计划", "warning");
            $("#planName").val("");
            $('#dd').dialog('close');
            return;
        }
    }
    // 获取计划名称
    var planName = $("#planName").val();
    var commitType = $("#commitType").val();
    $.ajax({
        type: 'post',
        dataType: 'json',
        url: 'platform/archivetemplatemanageController/savePlan?sysId='
            + sysId,
        data: {
            "planName": planName,
            "commitType": commitType,
        },
        success: function (data) {
            if (data.success == '1') {
                $("#dd").dialog('close');
                $("#planName").val("");
                parent.parent.parent.$.messager.show({
                    //消息
                    //保存成功!
                   /* title: commonLang.tip.message,*/
                	title:"消息",
                    msg: "保存成功",
                    timeout: 500,
                    showType: 'slide'
                });
                $('#dataTable').datagrid('reload');
            } else if (data.success == '0') {
                $("#dd").dialog('close');
                $('#dataTable').datagrid('reload');
                parent.parent.parent.$.messager.show({
                    //消息
                    //保存失败!
                    /*title: commonLang.tip.message,*/
                	title:"消息",
                    msg: "保存失败",
                    timeout: 500,
                    showType: 'slide'
                });
            }
        }
    });
}

/**
 * create by zxl 添加项目，请勿改动
 */
function saveProject() {
    // 在最后一行生成新增项目的input输入框
    var rows = $('#ProjectTable').datagrid('getRows');
    $('#ProjectTable').datagrid('insertRow', {
        index: rows.length + 1,
        row: {
            projectName: '项目名称',
        }
    });
    $("#ProjectTable").datagrid('beginEdit', rows.length - 1);
    // 记录开始编辑行
    if (beginRowCount == 1) {
        beginRowCount = rows.length - 1;
    }
}

/**
 * 删除项目
 */
function deleteProject() {
    var rows = $('#ProjectTable').datagrid('getSelections');
    if (rows.length <= 0) {
        delete rows;
        $.messager.alert("提示", "请选择要删除的记录？", "warning");
        return;
    }
    if (rows != null || rows.length > 0) {
        var arg = "";
        for (var i = 0; i < rows.length; i++) {
            if (i == rows.length - 1) {
                arg += "'" + rows[i].ID + "'";
            } else {
                arg += "'" + rows[i].ID + "',";
            }
        }
        deletePlanChecked(arg);
    }
}

function formatTooltip(val, row) {
    return "<a href='javascript:void(0)' onclick=\"toInfo(\'" + row.ID + "\',\'" + row.ARCHIVE_FILE_KIND_NAME + "\')\">" + val + "</a>"
}

function toInfo(id,planName) {
    planName = encodeURI(encodeURI(planName)); // 前端编码
    // top.addTab('查看详情', , "", "", " -0px -120px");
    var dialogId = "formPlanInfo";
    /*var formAdd = new MyStorageApplyDialog(dialogId, "800", "400",
        "", false, true, false, false, true);
    formAdd.show();*/
    var formEdit = new CommonDialog(dialogId, "800", "400",'avicit/im/lantop/basic/archivetemplatemanager/ArchivetemplatemanagerManagerList.jsp?id=' + id+'&planName='+planName,
        "查看详情", false, true, false);
    formEdit.show();
}
